import { Request, RequestHandler, Response } from "express";
import {
  IGetProjectDeclareByIdReq,
  IAddProjectDeclareReq,
  IUpdateProjectDeclareReq,
  IDeleteProjectDeclareReq,
} from "../../models/project-declare.model";
import * as ProjectDeclareService from "../services/project-declare.service";
import { convertToLocalDateString } from "../utils/date";

/**
 * Get active ProjectDeclare records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getProjectDeclares: RequestHandler = async (
  req: Request,
  res: Response
) => {
  try {
    const ProjectDeclares = await ProjectDeclareService.getProjectDeclares();

    res.status(200).json({
      data: ProjectDeclares,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[ProjectDeclares.controller][getProjectDeclares][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "项目申报列表获取失败！",
    });
  }
};

/**
 * Get ProjectDeclare record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getProjectDeclareById: RequestHandler = async (
  req: IGetProjectDeclareByIdReq,
  res: Response
) => {
  try {
    const ProjectDeclare = await ProjectDeclareService.getProjectDeclareById(req.params.id);

    res.status(200).json({
      data: ProjectDeclare,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[ProjectDeclares.controller][getProjectDeclareById][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "项目申报信息获取失败！",
    });
  }
};

/**
 * Inserts a new ProjectDeclare record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addProjectDeclare: RequestHandler = async (
  req: IAddProjectDeclareReq,
  res: Response
) => {
  try {
    req.body.createdTime = new Date(Date.now());
    req.body.updatedTime = req.body.createdTime;
    req.body.projectDeclareStart = req.body.projectDeclareStart
    ? convertToLocalDateString(req.body.projectDeclareStart)
    : null;
    req.body.projectDeclareEnd = req.body.projectDeclareEnd
    ? convertToLocalDateString(req.body.projectDeclareEnd)
    : null;
    const result = await ProjectDeclareService.insertProjectDeclare(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[ProjectDeclares.controller][addProjectDeclare][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    if (error.code === "ER_DUP_ENTRY") {
      res.status(500).json({
        message: "项目申报标题重复，请重新输入！",
      });
    } else {
      res.status(500).json({
        message: "项目申报信息保存失败！",
      });
    }
  }
};

/**
 * Updates existing ProjectDeclare record
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const updateProjectDeclareById: RequestHandler = async (
  req: IUpdateProjectDeclareReq,
  res: Response
) => {
  try {
    req.body.updatedTime = new Date(Date.now());
    req.body.projectDeclareStart = req.body.projectDeclareStart
      ? req.body.projectDeclareStart.length === 10 ?req.body.projectDeclareStart : convertToLocalDateString(req.body.projectDeclareStart)
      : null;
    req.body.projectDeclareEnd = req.body.projectDeclareEnd
      ? req.body.projectDeclareEnd.length === 10 ?req.body.projectDeclareEnd : convertToLocalDateString(req.body.projectDeclareEnd)
      : null;
    const result = await ProjectDeclareService.updateProjectDeclare({
      ...req.body,
      id: req.params.id,
    });

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[ProjectDeclares.controller][updateProjectDeclareById][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    if (error.code === "ER_DUP_ENTRY") {
      res.status(500).json({
        message: "项目申报标题重复，请重新输入！",
      });
    } else {
      res.status(500).json({
        message: "项目申报信息更新失败！",
      });
    }
  }
};

/**
 * deletes a ProjectDeclare
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteProjectDeclareById: RequestHandler = async (
  req: IDeleteProjectDeclareReq,
  res: Response
) => {
  try {
    const result = await ProjectDeclareService.deleteProjectDeclare(req.params.id);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[ProjectDeclares.controller][deleteProjectDeclareById][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "项目申报信息删除失败！",
    });
  }
};
